import { MultiVisitor, createRootAst } from "../ast";
import { tryRun } from "../utils";
import { RedLiveRoomVisitor } from "./RedLiveRoom";
import { RedOpenContext } from "./RedOpen";
import { RedOpenFailVisitor } from "./RedOpenFail";
import { RedOpenJoinVisitor } from "./RedOpenJoin";
import { RedOpenSuccessVisitor } from "./RedOpenSuccess";
import { RedOpenWaitVisitor } from "./RedOpenWait";

function toastLog(str: string) {
    events.broadcast.emit('UPDATE', str)
}

export function checkOpen() {
    const ctx: RedOpenContext = {}
    const visitor = new MultiVisitor([
        new RedOpenSuccessVisitor(),
        new RedOpenFailVisitor(),
        new RedOpenWaitVisitor(),
        new RedOpenJoinVisitor()
    ])
    const root = createRootAst()
    root.visit(visitor, ctx)
    return ctx;
}
export function getRedJoinHandler(setting: any) {
    const redOpenJoinVisitor = new RedOpenJoinVisitor()
    let total: number = 10;
    const ctx: RedOpenContext = {
        setting
    }
    while (total > 0) {
        const root = createRootAst()
        root.visit(redOpenJoinVisitor, ctx)
        if (ctx.join) {
            break;
        }
        sleep(1000)
        total -= 1;
    }
    if (!ctx.join) {
        toastLog(`无满足条件红包`)
        return;
    }
    return ctx.join;
}
export function getRedOpenHandler() {
    const redLiveRoomVisitor = new RedLiveRoomVisitor()
    let total: number = 10;
    const ctx: RedOpenContext = {}
    while (total > 0) {
        const root = createRootAst()
        root.visit(redLiveRoomVisitor, ctx)
        if (ctx.open) {
            break;
        }
        sleep(1000)
        total -= 1;
    }
    if (!ctx.open) {
        toastLog(`此房间无红包`)
        return;
    }
    return ctx.open;
}
export function hasWaitRedPacket() {
    const handler = getRedOpenHandler()
    if (!handler) return false;
    handler();
    sleep(1000)
    let total = 5;
    const ctx: RedOpenContext = {}
    const visitor = new RedOpenWaitVisitor()
    while (total > 0) {
        const root = createRootAst()
        root.visit(visitor, ctx)
        if (ctx.hasWait) {
            break;
        }
        sleep(1000)
        total -= 1;
    }
    return !!ctx.hasWait
}

export function waitRedOpen(setting: any) {
    let total = 60 * 10;
    const check = () => {
        const ctx: RedOpenContext = {
            setting,
            status: 'opening',
            count: 0,
        }
        const visitor = new MultiVisitor([
            new RedLiveRoomVisitor(),
            new RedOpenSuccessVisitor(),
            new RedOpenFailVisitor(),
            new RedOpenWaitVisitor(),
            new RedOpenJoinVisitor()
        ])
        visitor.setStop((ctx: RedOpenContext) => {
            if (ctx.open) return true;
            return false;
        })
        while (total > 0) {
            const root = createRootAst()
            root.visit(visitor, ctx)
            total -= 1;
            tryRun(() => {
                ctx.open && ctx.open();
                ctx.open = undefined;
                ctx.join && ctx.join()
                ctx.join = undefined;
            })
            if (ctx.status === 'fail') {
                break;
            }
            if (ctx.status === 'success') {
                break;
            }
            toastLog(`【${ctx.status}】等待开奖 ${total}`)
            sleep(1000)
        }
        tryRun(() => {
            ctx.close && ctx.close()
            ctx.close = undefined;
        })
        return ctx;
    }
    /**
     * 参与并等待开奖
     */
    let ctx = check()
    if (ctx.count > 0) {
        toastLog(`恭喜您中了${ctx.count}快币`)
        return ctx.count;
    }
    if (hasWaitRedPacket()) {
        let ctx = check()
        if (ctx.count > 0) {
            toastLog(`恭喜您中了${ctx.count}快币`)
            return ctx.count;
        }
    }
    // 可能一个房间同时参加多个红包
    toastLog(`很遗憾，没有中奖`)
    return ctx.count;
}
